/*
 * LineManagerCase.cpp
 *
 *  Created on: 2012-9-9
 *      Author: yangentao@gmail.com
 */

#include "test.h"

#include "../ed/LineManager.h"
#include "../dep.h"

class LineMgrTestReplace: public LineManager, public testing::Test {
public:
	void SetUp() {
	}
	void TearDown() {
	}
public:
	void replace(int from, int len, const String& newText) {
		String s(' ', len);

		text.replace(from, len, newText);
		onTextReplaced(from, s, newText);
	}
public:
	String text;
};

TEST_F(LineMgrTestReplace, replace1) {
	replace(0, 0, "abc\nd\nef");
	EXPECT_EQ(vec.length(), 3+1);
	EXPECT_EQ(count(), 3);
	EXPECT_EQ(getLineStart(0), 0);
	EXPECT_EQ(getLineStart(1), 4);
	EXPECT_EQ(getLineStart(2), 6);
}

TEST_F(LineMgrTestReplace, replace2) {
	replace(0, 0, "abc\nd\nef");
	replace(3, 1, ""); //abcd\nef
	EXPECT_EQ(vec.length(), 2+1);
	EXPECT_EQ(count(), 2);
	EXPECT_EQ(getLineStart(0), 0);
	EXPECT_EQ(getLineStart(1), 5);
}

TEST_F(LineMgrTestReplace, replace3) {
	replace(0, 0, "abc\nd\nef"); //abcNdNef
	replace(3, 1, "\n\n"); //abcNNdNef
	EXPECT_EQ(vec.length(), 4+1);
	EXPECT_EQ(count(), 4);
	EXPECT_EQ(getLineStart(0), 0);
	EXPECT_EQ(getLineStart(1), 4);
	EXPECT_EQ(getLineStart(2), 5);
	EXPECT_EQ(getLineStart(3), 7);
}

TEST_F(LineMgrTestReplace, replace4) {
	replace(0, 0, "abc\nd\nef"); //abcNdNef
	replace(3, 3, "\n"); //abcNef
	EXPECT_EQ(vec.length(), 2+1);
	EXPECT_EQ(count(), 2);
	EXPECT_EQ(getLineStart(0), 0);
	EXPECT_EQ(getLineStart(1), 4);
}

TEST_F(LineMgrTestReplace, replace5) {
	replace(0, 0, "a"); //a
	replace(1, 0, "\n"); //aN
	replace(0, 2, "a\nb\nc\n"); //aNbNcN
	EXPECT_EQ(vec.length(), 4+1);
	EXPECT_EQ(count(), 4);
	EXPECT_EQ(getLineStart(0), 0);
	EXPECT_EQ(getLineStart(1), 2);
	EXPECT_EQ(getLineStart(2), 4);
	EXPECT_EQ(getLineStart(3), 6);
}

TEST_F(LineMgrTestReplace, replace6) {
	replace(0, 0, "a"); //a
	replace(1, 0, "\n"); //aN
	replace(0, 2, "a\nb\nc\n"); //aNbNcN
	EXPECT_EQ(vec.length(), 4+1);
	EXPECT_EQ(count(), 4);
	EXPECT_EQ(getLineStart(0), 0);
	EXPECT_EQ(getLineStart(1), 2);
	EXPECT_EQ(getLineStart(2), 4);
	EXPECT_EQ(getLineStart(3), 6);

	replace(0, 5, "a"); //aN
	EXPECT_EQ(vec.length(), 2+1);
	EXPECT_EQ(count(), 2);
	EXPECT_EQ(getLineStart(0), 0);
	EXPECT_EQ(getLineStart(1), 2);

}

class LineMgrTest: public LineManager, public testing::Test {
public:
	void SetUp() {
		int arr[] = { 10, 20, 30 };
		vec.insert(1, arr, 3);
	}
	void TearDown() {
		vec.clear();
	}
};

TEST_F(LineMgrTest, getLineStart) {
	int n = getLineStart(0);
	EXPECT_EQ(n, 0);
	n = getLineStart(1);
	EXPECT_EQ(n, 10);
	n = getLineStart(2);
	EXPECT_EQ(n, 20);
	n = getLineStart(3);
	EXPECT_EQ(n, 30);

	EXPECT_EQ(count(), 4);

}

TEST_F(LineMgrTest, findLine) {

	int n = findLine(0);
	EXPECT_EQ(n, 0);

	n = findLine(1);
	EXPECT_EQ(n, 0);

	n = findLine(10);
	EXPECT_EQ(n, 1);

	n = findLine(15);
	EXPECT_EQ(n, 1);

	n = findLine(20);
	EXPECT_EQ(n, 2);

	n = findLine(29);
	EXPECT_EQ(n, 2);

	n = findLine(99);
	EXPECT_EQ(n, 3);

}

